<?php
class Book_info_model extends CI_Model{

	function getAllBookInfo(){
		$data=$this->db->query('select * from book_info');
		return $data;
	}

	function getFilterBookInfo(){
		$data=array(
		//'$employeid'=>$_POST['$employeid'],
				'bookday'=>$_POST['bookday']
		);
		$employeid = "2"; //第一个参数
		$bookday = array_shift($data); //第二个参数
		//$data['query']=$this->db->query('select a.id,a.employeid,a.bookday,a.booktime,a.status,b.custna,c.serverna,d.employena from book_info a left join cust_info b on a.custid=b.custid left join  server_info c on a.serverid = c.serverid left join employe_info d on a.employeid = d.employeid');
		$sql = "select a.id,a.employeid,a.bookday,a.booktime,a.status,b.custna,c.serverna,d.employena from book_info a left join cust_info b on a.custid=b.custid left join  server_info c on a.serverid = c.serverid left join employe_info d on a.employeid = d.employeid where 1=1";
		//$sql = "select * from book_info where 1=1";
		if(null != $bookday && "" != $bookday){
			$sql = $sql." and a.bookday='$bookday'";
		}
		if(null != $employeid && "" != $employeid){
			$sql = $sql." and d.employeid='$employeid'";
		}
		$data=$this->db->query($sql);
		return $data;
	}

	function delBookInfo($id){
		$this->db->where('id',$id);
		$data=$this->db->delete('book_info');
		return $data;
	}

	function getBookInfo($id){
		$this->db->where('id',$id);
		$data=$this->db->get('book_info');
		return $data;
	}

	function addBookInfo(){
		$serverid = $_POST['serverid'];
		$query=$this->db->query('select servertime from server_info where serverid = '.$serverid);
		$row = $query->row_array();
		$servertime = $row['servertime']; //服务时间

		$booktime = $_POST['booktime'];
		$arrtime = split(":",$booktime);
		$startnum = $arrtime[1]/60+$arrtime[0];
		$endnum = $arrtime[1]/60+$arrtime[0]+$servertime;
			
		$strtime = strval($endnum);
		if(!strpos($strtime,"."))
		$strtime=$strtime.".0";
			
		$arrendtime = split("\.",$strtime);
		$endss=$arrendtime[1]*6;
		$endtime = $arrendtime[0].":".$endss;

		$data=array(
				'custid'=>$_POST['custid'],
				'serverid'=>$serverid,
				'employeid'=>$_POST['employeid'],				
				'bookday'=>$_POST['bookday'], 
				'booktime'=>$booktime,
				'endtime'=>$endtime,
				'startnum'=>$startnum,
				'endnum'=>$endnum,
				'status'=>'0'
				);
				$this->db->insert('book_info',$data);
				return $this->db->insert_id();
	}

	function  updateBookInfo($id){
		$data=array(
				'custid'=>$_POST['custid'],
				'serverid'=>$_POST['serverid'],
				'employeid'=>$_POST['employeid'],
				'booktime'=>$_POST['booktime'],
				'bookday'=>$_POST['bookday'], 
				'status'=>'0'
				);
				$this->db->where('$id',$id);
				$this->db->update('book_info',$data);
	}

	function checkIsEmployeWorkDay($workday,$employeid){
		$arr=split("/", $workday);
		$time=mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
		$w = date("w",$time);
			
		$this->db->where('workday',$w);
		$this->db->where('employeid',$employeid);

		$data=$this->db->get('emp_workday');
		if ($data->num_rows() > 0)
		return true;
		else
		return false;
	}

	function checkIsHoliday($holidaytime){
		$this->db->where('holidaytime',$holidaytime);
		$data=$this->db->get('holiday_info');

		if ($data->num_rows() > 0)
		return true;
		else
		return false;
	}

	function getWorkTimeAndBookedTime($workday,$employeid){
		$arr=split("/", $workday);
		$time=mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
		$w = date("w",$time);
			
		$this->db->where('workday',$w);
		$this->db->where('employeid',$employeid);
		$query=$this->db->get('emp_workday');
		$row = $query->row_array();
		 
		$worktime = $row['shour'].":".$row['sminute']."-".$row['ehour'].":".$row['eminute'];

		//bookedtime
		$bookedtime="";
			
		$query=$this->db->query("select booktime,endtime from book_info where bookday='$workday' and employeid='$employeid' order by startnum");
		if ($query->num_rows() > 0){
			foreach($query->result() as $row)
			{
				$bookedtime=$bookedtime.",".$row->booktime."-".$row->endtime ;
			}
			$bookedtime=substr($bookedtime,1);
		}
		return $worktime."_".$bookedtime;
	}

	function isWorkTime($workday,$employeid,$worktime){
		$arrtime = split(":", $worktime);
		$arrmm = $arrtime[0] + 0;
		$arrss = $arrtime[1] + 0;

		$arr=split("/", $workday);
		$time=mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
		$w = date("w",$time);
			
		$this->db->where('workday',$w);
		$this->db->where('employeid',$employeid);
		$query=$this->db->get('emp_workday');
		$row = $query->row_array();


		$shour = $row['shour'];
		$sminute = $row['sminute'];
		$ehour = $row['ehour'];
		$eminute = $row['eminute'];

		if( ($arrmm>$shour || ($arrmm==$shour && $arrss>=$sminute))
		&& ($arrmm<$ehour || ($arrmm==$ehour && $arrss<=$eminute)))
		return true;
		else
		return false;
	}

	function checkIsEmployeFreeTime($bookday,$booktime,$employeid,$serverid){
		$query=$this->db->query('select servertime from server_info where serverid = '.$serverid);
		$row = $query->row_array();
		$servertime = $row['servertime']; //服务时间

		$arrtime = split(":",$booktime);
		$startnum = $arrtime[1]/60+$arrtime[0];
		$endnum = $arrtime[1]/60+$arrtime[0]+$servertime;
		$sql = "select id from book_info where employeid = $employeid and bookday ='$bookday' and ((startnum <= $startnum and endnum > $startnum) or (startnum < $endnum and endnum > $endnum))";

		$data = $this->db->query($sql);
		if ($data->num_rows() > 0)
		return false;
		else
		return true;
	}

	function getEmailMsg($id){
		$data=$this->db->query('select a.bookday, a.booktime,b.email,d.serverna,c.custna,b.employena from book_info a, employe_info b,cust_info c,server_info d where a.serverid = d.serverid and a.employeid=b.employeid and a.custid= c.custid and a.id='.$id);
		return $data;
	}

}
?>